package castledefense.gui;

import castledefense.data.GameData;
import de.matthiasmann.twl.Event;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Widget;

import twl.global.widget.GlobalButton;

public class SpellButton extends GlobalButton{
	
	private int type;
	private SpellPanel panel;
	
	private Widget loadBar;
	
	private String[] toolTipText = {
			"Pyromancer: 50% damage to all units and your own castle",
			"Flood: 20% damage and 50% slow for 7s to all units",
			"Earthquake: 20% damage and 2s stun to all units",
			"Blessed Wind: 100% extra damage to all towers for 10s" };
	
	public SpellButton(String name, int type, SpellPanel panel) {
		super(name);
		this.type = type;
		this.panel = panel;
		setSize(64, 18);
		setTheme("spellButtonTheme");
		setTooltipContent(toolTipText[type]);
		
		loadBar = new Widget();
		String theme = "";
		switch(type){
		case(GameData.fire):
			theme = "fillRectRed";
			break;
		case(GameData.water):
			theme = "fillRectBlue";
			break;
		case(GameData.earth):
			theme = "fillRectBrown";
			break;
		case(GameData.air):
			theme = "fillRectYellow";
			break;
		}
		loadBar.setTheme(theme);
		loadBar.setPosition(getInnerX(), getInnerY());
		add(loadBar);
		
	}
	
	@Override
	protected void paint(GUI gui){
		float coolDown = panel.getCooldownPercent(type);
		int loadWidth = (int) ((coolDown)*getInnerWidth());
		loadBar.setSize(loadWidth, getInnerHeight());
		getAnimationState().setAnimationState(STATE_DISABLED, coolDown != 1);
		paintBackground(gui);
		paintChildren(gui);
		if(coolDown == 1){
			paintWidget(gui);
		}
		paintOverlay(gui);
	}
	
	//---------------------- Input Event Handling -----------------------------
	
	@Override
	protected boolean handleEvent(Event evt){
		boolean result = super.handleEvent(evt);
		if(evt.getType() == Event.Type.MOUSE_BTNUP){
			if(evt.getMouseButton() == Event.MOUSE_LBUTTON){
				if(getModel().isHover()){
					isClicked();
				}
			}
		}
		return result;
	}
	
	private void isClicked() {
		panel.activate(this.type);		
	}

}
